package com.ecoolex.discount.sales.core.manager;

import com.ecoolex.discount.sales.core.dao.DiscountBrandClassifyDAO;
import com.ecoolex.discount.sales.entity.DiscountBrandClassify;
import com.ecoolex.discount.sales.entity.DiscountBrandClassifyExample;
import com.ecoolex.discount.sales.pojo.AddDiscountBrandClassifyDTO;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.CollectorUtil;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DiscountBrandClassifyManager {
	
    @Autowired
    private DiscountBrandClassifyDAO dao;
    
    public void update(Integer brandId,List<AddDiscountBrandClassifyDTO> list,String createUser) {
    	List<DiscountBrandClassify> oldList = getByBrandId(brandId);
    	for (AddDiscountBrandClassifyDTO dto : list) {
    		//新增
    		if(Check.isNullOrEqZero(dto.getId())) {
    			DiscountBrandClassify record = new DiscountBrandClassify();
    			record.setBrandId(brandId);
    			record.setClassifyId(dto.getClassifyId());
    			record.setBrandWeight(dto.getBrandWeight());
    			record.setDeleted(DiscountBrandClassify.NOT_DELETED);
    			record.setCreateUser(createUser);
    			record.setCreateTime(Date.from(Instant.now()));
    			record.setUpdateUser(createUser);
    			record.setUpdateTime(Date.from(Instant.now()));
    			dao.insert(record);
    		}else {
    			DiscountBrandClassify old = oldList.stream().collect(CollectorUtil.first(o -> dto.getId().equals(o.getId())));
    			BizAssert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(20), "品牌分类关系id={}不存在",dto.getId());
    			old.setClassifyId(dto.getClassifyId());
    			old.setBrandWeight(dto.getBrandWeight());
    			old.setUpdateUser(createUser);
    			old.setUpdateTime(Date.from(Instant.now()));
    			dao.updateByPrimaryKey(old);
    			oldList.remove(old);
    		}
		}
    	oldList.forEach(old -> delete(old, createUser));
    }
    
    public void delete(DiscountBrandClassify record,String updateUser) {
    	record.setDeleted(DiscountBrandClassify.IS_DELETED);
    	record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKey(record);
    }
    
    public void deleteByBrandId(Integer brandId,String updateUser) {
    	List<DiscountBrandClassify> list = getByBrandId(brandId);
    	if(Check.notNullOrEmpty(list)) {
    		for (DiscountBrandClassify discountBrandClassify : list) {
    			delete(discountBrandClassify,updateUser);
			}
    	}
    }
    
    public DiscountBrandClassify get(Integer id) {
    	return dao.selectByPrimaryKeyWithLogicalDelete(id, DiscountBrandClassify.NOT_DELETED);
    }
    
    public List<DiscountBrandClassify> getByBrandId(Integer brandId){
    	DiscountBrandClassifyExample example = new DiscountBrandClassifyExample();
    	example.createCriteria().andBrandIdEqualTo(brandId).andDeletedEqualTo(DiscountBrandClassify.NOT_DELETED);
    	return dao.selectByExample(example);
    }
    
    public List<DiscountBrandClassify> getByClassifyId(Integer classifyId){
    	DiscountBrandClassifyExample example = new DiscountBrandClassifyExample();
    	example.createCriteria().andClassifyIdEqualTo(classifyId).andDeletedEqualTo(DiscountBrandClassify.NOT_DELETED);
    	return dao.selectByExample(example);
    }
    
    public DiscountBrandClassify getByClassifyIdAndBrandId(Integer classifyId,Integer brandId) {
    	DiscountBrandClassifyExample example = new DiscountBrandClassifyExample();
    	example.createCriteria().andClassifyIdEqualTo(classifyId).andBrandIdEqualTo(brandId).andDeletedEqualTo(DiscountBrandClassify.NOT_DELETED);
    	return dao.selectOneByExample(example);
    }
}